package com.main.tripplanner.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.main.tripplanner.dao.AttractionDAO;
import com.main.tripplanner.dao.AttractionQuestionDAO;
import com.main.tripplanner.dao.RouteDAO;
import com.main.tripplanner.persistent.Attraction;
import com.main.tripplanner.persistent.Attraction.AttractionType;
import com.main.tripplanner.persistent.AttractionQuestion;
import com.main.tripplanner.persistent.City;
import com.main.tripplanner.persistent.Country;
import com.main.tripplanner.persistent.Route;
import com.main.tripplanner.service.AbstractService;
import com.main.tripplanner.service.AttractionService;


@Service
public class AttractionServiceImpl extends AbstractService implements AttractionService{
	@Autowired
	private AttractionDAO attractionDAO;
	@Autowired
	private AttractionQuestionDAO attractionQuestionDAO;
	@Autowired 
	private RouteDAO routeDAO;

	public City getCityByAttraction(Country country,  Attraction attraction){
		return null;
	}

	public Attraction getAttractionMaxEnjoyment(Country country, Route route, AttractionType attractionType) {
		List<Integer> attractionsIds = getEntityIds(route.getAttractions());
		List<Integer> citiesIds = getEntityIds(route.getCities());
		List<Attraction> attractionsList = attractionDAO.getAllAttractionsByCountryAndType(country, citiesIds, attractionsIds, attractionType);
		Attraction maxAttraction = new Attraction();
		for (Attraction attraction : attractionsList) {
			if(maxAttraction.getEnjoymentLevel() < attraction.getEnjoymentLevel()){
				maxAttraction = attraction;
			}
		}
		return maxAttraction;
	}

	public int calculateAttractionPotential(int enjoymentLevelToBeat, Attraction attraction) {
		float up =(enjoymentLevelToBeat*attraction.getEnjoymentLevelVotes() - attraction.getEnjoymentLevel());
		float down = (10-enjoymentLevelToBeat);
		Double result = Math.ceil(up/down);
		return result.intValue();
	}



	public List<Attraction> getCityAttractions(City city, Attraction bestAttraction) {
		List<Attraction> list = attractionDAO.getCityAttractions(city, bestAttraction);
		return list;
	}

	public AttractionQuestion getNextAttractionQuestion(Country country) {
		AttractionQuestion attractionQuestion = attractionQuestionDAO.getNextAttractionQuestion(country);
		return attractionQuestion;
	}

	@Override
	public void Vote(Attraction attraction, int value) {
		// TODO Auto-generated method stub
		
	}

	/*public void Vote(Attraction attraction, int value) {
		attraction.setEnjoymentLevel((attraction.getEnjoymentLevel()*attraction.getEnjoymentLevelVotes() + value) / attraction.getEnjoymentLevelVotes() + 1);
		attraction.setEnjoymentLevelVotes(attraction.getEnjoymentLevelVotes() + 1);
		List<AttractionQuestion> attractionQuestionlist = attractionQuestionDAO.getAttractionQuestionByBestReplacementAttraction(attraction);
		if(!attractionQuestionlist.isEmpty()){
			for (AttractionQuestion attractionQuestion : attractionQuestionlist) {
				Attraction currentBestAttraction = attractionQuestion.getCurrentAttraction();
				if(attraction.getEnjoymentLevel() > currentBestAttraction.getEnjoymentLevel()){
					Route route = routeDAO.getRouteByCountry(attractionQuestion.getCountry());
					route.setToReplan(true);
					routeDAO.updateEntity(route);
				}
			}
		}

	}*/




}
